package com.ddtech.business.web.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.web.entity.TeacherUserRel;
import com.ddtech.business.web.entity.WebUser;
import com.ddtech.business.web.mapper.WebUserMapper;
import com.ddtech.business.web.query.WebUserQuery;
import com.ddtech.business.web.res.UserTeacherRelVo;
import com.ddtech.business.web.service.TeacherUserRelService;
import com.ddtech.business.web.service.WebUserService;
import com.ddtech.framework.mapper.UserVoMapper;
import com.ddtech.framework.vo.UserVo;
import com.ddtech.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bin
 */
@Service
public class WebUserServiceImpl extends ServiceImpl<WebUserMapper, WebUser> implements WebUserService {
    @Resource
    private UserVoMapper userVoMapper;
    @Autowired
    private TeacherUserRelService teacherUserRelService;

    @Override
    public Object pageList(WebUserQuery webUserQuery) {
        Page page = new Page(webUserQuery.getPage(), webUserQuery.getSize());
        IPage<UserVo> userPage = userVoMapper.findUserPage(page, webUserQuery);
        return PageUtil.toPage(userPage.getRecords(), userPage.getTotal());
    }

    @Override
    public Object getListRel(WebUserQuery webUserQuery) {
        Page page = new Page(webUserQuery.getPage(), webUserQuery.getSize());
        IPage<UserVo> userPage = userVoMapper.findUserPage(page, webUserQuery);
        List<UserVo> records = userPage.getRecords();
        List<UserVo> collect = new ArrayList<>();
        if (CollUtil.isNotEmpty(records)) {
            collect = records.stream().map(item -> {
                String id = item.getId();
                QueryWrapper<TeacherUserRel> telQuery = new QueryWrapper<>();
                telQuery.eq(TeacherUserRel.USER_ID, id);
                List<TeacherUserRel> rel = teacherUserRelService.list(telQuery);
                if (CollUtil.isNotEmpty(rel)) {
                    List<UserVo> teacherList = rel.stream().map(userRel -> {
                        UserVo teacher = userVoMapper.findUserById(userRel.getTeacherId());
                        return teacher;
                    }).collect(Collectors.toList());
                    item.setTeacher(teacherList);
                }
                return item;
            }).collect(Collectors.toList());
        }
        return PageUtil.toPage(collect, userPage.getTotal());
    }

    @Override
    public void createRel(UserTeacherRelVo userTeacherRelVo) {
        QueryWrapper<TeacherUserRel> telQuery = new QueryWrapper<>();
        telQuery.eq(TeacherUserRel.USER_ID, userTeacherRelVo.getUserId());
        teacherUserRelService.remove(telQuery);
        userTeacherRelVo.getTeacherId().forEach(item -> {
            TeacherUserRel rel = new TeacherUserRel();
            rel.setUserId(userTeacherRelVo.getUserId());
            rel.setTeacherId(item);
            teacherUserRelService.save(rel);
        });


    }
}
